import { startsWith, endsWith } from 'lodash-es';
import { $isNullOrEmpty } from './index';
const isTrue = (value, condition, selectValue, isSwiper = false)=> {
  let selected = isSwiper ? selectValue  :  $isNullOrEmpty(selectValue) ? '' : selectValue.join('') ;
  let conditionResult = false;
  switch (condition) {
    case 'IS_NULL':
      conditionResult = $isNullOrEmpty(selected);
      break;
    case 'IS_NOT_NULL':
      conditionResult = !$isNullOrEmpty(selected);
      break;
    case 'BELONG':
      conditionResult = selected === value;
      break;
    case 'NOT_BELONG':
      conditionResult = selected !== value;
      break;
    case 'CONTAIN':
      conditionResult = (selected.indexOf(value) !== -1);
      break;
    case 'NOT_CONTAIN':
      conditionResult = (selected.indexOf(value) === -1);
      break;
    case 'PRE_IS':
      conditionResult = startsWith(selected, value);
      break;
    case 'PRE_NOT_IS':
      conditionResult = !startsWith(selected, value);
      break;
    case 'END_IS':
      conditionResult = endsWith(selected, value);
      break;
    case 'END_NOT_IS':
      conditionResult = !endsWith(selected, value);
      break;
    default:
      conditionResult = false;
  }
  return conditionResult;
};
export function getConditions (currentComponentId, coditionData, filterValue, selectValue, componentKey) {
  if (!coditionData) {
    return true;
  }
  const { and = [], or = [] } = coditionData || {};
  if( !and.length && !or.length) {
    return true;
  }
  const condition = and.length ? and : or;
  const conditionDate = condition.length && condition.map( item =>
      {
          if(componentKey === 'media_swiper') {
            return isTrue(item.selectedPicture, item.condition, selectValue, true)
          }
          selectValue = currentComponentId != item.componentId ?  filterValue[item.componentId] : selectValue;
          return isTrue(item.inputText, item.condition, selectValue)
      }) || [];
      //判断是and 还是 or
  const isFalse =  and.length ? conditionDate.every(item => { return item }) : conditionDate.some(item => { return item });
    return isFalse;
}
